package studychinese.activity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.hschinese.pronunciation.R;
import com.hschinese.pronunciation.activity.BaseActivity;
import com.hschinese.pronunciation.activity.MyApplication;
import com.hschinese.pronunciation.activity.StudyConstructionActivity;
import com.hschinese.pronunciation.interfaces.LessonProgressInterface;
import com.hschinese.pronunciation.interfaces.NextLessonInterface;
import com.hschinese.pronunciation.interfaces.RecordStatusListener;
import com.hschinese.pronunciation.interfaces.VolumeListener;
import com.hschinese.pronunciation.utils.NumberFormatUtil;
import com.hschinese.pronunciation.widget.ComplexWordView;
import com.hschinese.pronunciation.widget.GestureListener;
import com.jaeger.library.StatusBarUtil;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.hschinese.pronunciation.R.id.study_progress;


public class ComplexWordActivity extends BaseActivity {
    @Bind(R.id.study_quit)
    ImageView studyQuit;
    @Bind(study_progress)
    ProgressBar studyProgress;
    @Bind(R.id.study_progress_layout)
    LinearLayout studyProgressLayout;
    @Bind(R.id.tv_process)
    TextView tvProcess;
    @Bind(R.id.study_main_view)
    LinearLayout studyMainView;
    @Bind(R.id.basestrokesview)
    ComplexWordView basestrokesview;


    // 0第一个页面
    private int mCurrentPosition = 0;
    private NextLessonInterface mNextListener;
    private MainGestrueListener mGestureListener;
    private boolean isStarted = true;
    private RecordStatusListener mRecordListener;
    private VolumeListener mVolumeListener;
    //    private MyPopupWindow mPopupWindow;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_compex_word);
        ButterKnife.bind(this);
        Intent it = getIntent();
        // 启动出决定开始的是哪个课程
        mCurrentPosition = it.getIntExtra("position", 0);
        setNextListener();
        mGestureListener = new MainGestrueListener(ComplexWordActivity.this);
        initViews();
        isStarted = false;
    }
    @Override
    protected void setStatusBar() {
        StatusBarUtil.setTranslucentForImageView(this, 0, studyMainView);
    }
    private void initViews() {
        studyMainView.setLongClickable(true);
        studyMainView.setOnTouchListener(mGestureListener);
        basestrokesview.setNextListener(mNextListener);
        basestrokesview.setListener(new LessonProgressInterface() {
            @Override
            public void setProgress(int total, int position) {
                studyProgress.setMax(total);
                studyProgress.setProgress(position + 1);
                tvProcess.setText(NumberFormatUtil.getPercent(position, total) + "%");
            }
        });
        basestrokesview.setData(mCurrentPosition);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        basestrokesview.destory();
    }

    private void setNextListener() {
        mNextListener = new NextLessonInterface() {
            @Override
            public void onLessonChanged(int currentLesson) {
                mCurrentPosition = currentLesson;
                // if (mCurrentLesson <= 0) return;
               /* if (currentLesson == 2) {
                    if (mPractiseView != null) {
                        mPractiseView.onDestroy();
                        mContainerView.removeView(mPractiseView);
                    }
                    initStudyData();
                } else if (currentLesson == 1 || currentLesson == 3) {
                    if (mStudyWordsView != null) {
                        mStudyWordsView.onDestroy();
                        mContainerView.removeView(mStudyWordsView);

                    }

                } else if (currentLesson == 9 || currentLesson == 8) {
                    if (mDictationPracticeView != null) {
                        mDictationPracticeView.onDestroy();
                        mContainerView.removeView(mDictationPracticeView);
                    }

                } else if (currentLesson == 7) {
                    if (mKnowledgePointView != null) {
                        mKnowledgePointView.onDestroy();
                        mContainerView.removeView(mKnowledgePointView);
                    }

                } else if (currentLesson == 4) {
                    Intent it = new Intent(StudyConstructionActivity.this, WordListActivity.class);
                    it.putExtra("lessonType", currentLesson);
                    startActivity(it);
                    finish();
                } else if (currentLesson == 5) {
                    Intent it = new Intent(StudyConstructionActivity.this, ConsListActivity.class);
                    it.putExtra("lessonType", currentLesson);
                    startActivity(it);
                    finish();
                } else if (currentLesson == 6) {
                    if (mConsonantStudyView != null) {
                        mConsonantStudyView.onDestroy();
                        mContainerView.removeView(mConsonantStudyView);
                    }

                } else if (currentLesson >= 7) {
                    // 结束当前Activity，进入关卡页
                    finish();
                }*/
            }
        };
    }


    @Override
    public void onBackPressed() {
        this.finish();
        /*if (mStudyWordsView != null) {
            mStudyWordsView.onDestroy();
        }*/

    }

    private void gotoNextWord() {
       /* if (mCurrentLesson == 13|| mCurrentLesson == 2) {
            mStudyWordsView.nextWord();
        }*/
       if (mCurrentPosition>=50){
           //弹出提示，去到下个模块
           showAlert();
       }else{
           basestrokesview.resetProgressBar();
           basestrokesview.nextWord();
       }
    }

    private void showAlert() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(R.string.next_lesson_dialog_msg);
        builder.setPositiveButton(R.string.btn_confirm, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                startActivity(new Intent( ComplexWordActivity.this,StudyConstructionActivity.class).putExtra("lesson",13).putExtra("weith",0));
                MyApplication.getInstance().removeAndFinish(StrokesListActivity.class);
                ComplexWordActivity.this.finish();
            }
        });
        builder.setNegativeButton(R.string.btn_cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                ComplexWordActivity.this.finish();
            }
        });
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }
    @OnClick({R.id.study_quit, R.id.tv_process})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.study_quit:
                this.finish();
                break;
            case R.id.tv_process:
                break;
        }
    }


    class MainGestrueListener extends GestureListener {

        public MainGestrueListener(Context context) {
            super(context);
        }

        @Override
        public boolean left() {
            gotoNextWord();
            return true;
        }

        @Override
        public boolean right() {
            basestrokesview.resetProgressBar();
            basestrokesview.beforeWord();
            return true;
        }
    }


}
